let global_data = {
    splashAdObj: {
        'huawei': '4112906975626937,1102303915336106,6172800915539452',
    },
    splashAd: null,
    splashAdList: [],
    idIndex: 0
}
/**
 * ylh开屏
 * @param isAppHide
 */
const initYlhSplashSdk = (_this, isAppHide = false) => {
    if (global_data.splashAdObj) {
        global_data.splashAdList = global_data.splashAdObj[_this.brand].split(',')
        onLoadSplashAd(_this, isAppHide)
    }
}
const onLoadSplashAd = (_this, isAppHide) => {
    if (global_data.idIndex >= global_data.splashAdList.length) {
        if (isAppHide) {
            global_data.idIndex = 0
        } else {
            return
        }
    }
    let placementId = global_data.splashAdList[global_data.idIndex]
    global_data.idIndex++
    _this.$def.ylh_sdk && _this.$def.ylh_sdk.onYlhReady(() => {
        try {
            let param = {
                id: placementId,
                adTurn: 1,
                layer: 0,
                ad_network: 18,
                scenario: '优量汇',
                ad_type: '优量汇',
                type: 'ylh',
                popName: 'ylh开屏',
                popGroupName: 'ylh开屏',
                requestNum: global_data.idIndex,
                scene: '自动请求',
                ad_type: 'ylh开屏',
                result: '发起请求'
            }
            AD_SDK.uploadAdInfo('request', param)
            global_data.splashAd = _this.$def.ylh_sdk.createSplashAd({
                placementId,
            });
            global_data.splashAd.onLoad(() => {
                console.log(placementId, 'ylh开屏load')
                param.result = '有广告'
                global_data.idIndex = 0
                AD_SDK.uploadAdInfo('request', param)
                AD_SDK.uploadAdInfo('render', param)
                global_data.splashAd.show()
                // 通知已订阅
                AD_SDK.eventEmitter.emit('ylhLoad');
            });
            global_data.splashAd.onError((err = {}) => {
                // 广告异常回调
                console.log(placementId, err, 'ylh开屏error')
                param.result = `无广告${err && err.errorCode}`
                AD_SDK.uploadAdInfo('request', param)
                onLoadSplashAd(_this)
            });
            global_data.splashAd.onExpose(() => {
                // 广告曝光回调
                console.log('ylh开屏show')
                AD_SDK.uploadAdInfo('show', param)
            });
            let flag = false
            global_data.splashAd.onClick(() => {
                if (flag) {
                    return
                }
                flag = true
                // 广告点击回调
                console.log('ylh开屏click')
                AD_SDK.uploadAdInfo('click', param)
            });
            global_data.splashAd.onClose(() => {
                // 广告关闭回调
                console.log('ylh开屏close')
                onDestroySplashAd()
            });
            // 第三步：load 广告数据
            global_data.splashAd.load();
        } catch (error) {
            console.log(error, 'ylh开屏创建异常');
        }
    })
}
const onDestroySplashAd = () => {
    if (global_data.splashAd) {
        global_data.splashAd.destroy();
        global_data.splashAd = null;
    }
}
export { initYlhSplashSdk }